import rules from '@/common/ability/rules'
import { validateUtils } from '@/common/utils/index'
let validate = {
  // 查询页字段
  query: {
    voteName: ['voteName', {}], // 投票名称
    Time: ['Time', {}], // 发布时间
    state: ['state', { initialValue: '-1' }] // 状态
  },
  // 新增业字段 基本信息
  addBasic: {
    email: [
      'email',
      {
        rules: [
          { required: true, whitespace: true, message: '请输入邮箱！' },
          { pattern: /^[0-9]*$/, message: '邮箱格式不正确！' }
        ]
      }
    ],
    sfzh: [
      'sfzh',
      {
        rules: [
          { required: true, whitespace: true, message: '请输入身份证号' },
          {
            validator: (rule, value, callback) => {
              if (!value) {
                callback()
              } else {
                let validateFlag = validateUtils.checkIDCard(value)
                if (validateFlag) {
                  callback()
                } else {
                  // eslint-disable-next-line standard/no-callback-literal
                  callback('请输入正确的身份证号')
                }
              }
            }
          }
        ]
      }
    ],
    password: [
      'password',
      {
        rules: [
          { required: false, whitespace: true, message: '请输入密码！' }
        ]
      }
    ],
    select: [
      'select',
      {
        rules: [
          { required: true, message: '请选择！' }
        ]
      }
    ],
    time: [
      'time',
      {
        rules: []
      }
    ],
    range: [
      'range',
      {
        rules: []
      }
    ]
  }
}

// 遍历所有的检验字段,检查每个字段的option,为每个option添加公用的规则
for (let key in validate) {
  for (let k in validate[key]) {
    validate[key][k][1] = rules.addNormalize(validate[key][k][1])
  }
}

export default validate
